import * as vscode from 'vscode';
import { MavenDependencyAnalyzer } from './dependencyAnalyzer';
import { MavenRunner } from './mavenRunner';
import { TestRunner } from './testRunner';
import { MavenTerminal } from './mavenTerminal';
import { MavenModulesProvider } from './views/mavenModulesView';
import { DependencyAnalyzerProvider } from './views/dependencyAnalyzerView';
import { MavenUtils } from './mavenUtils';

export function activate(context: vscode.ExtensionContext) {
    console.log('Maven Helper extension is now active');

    try {
        // 初始化各功能模块
        const dependencyAnalyzer = new MavenDependencyAnalyzer(context);
        const mavenRunner = new MavenRunner();
        const testRunner = new TestRunner();
        const mavenTerminal = new MavenTerminal();
        const mavenUtils = new MavenUtils();
        
        // 初始化视图提供者
        const mavenModulesProvider = new MavenModulesProvider();
        const dependencyAnalyzerProvider = new DependencyAnalyzerProvider();

        // 注册树视图
        vscode.window.registerTreeDataProvider('maven-modules', mavenModulesProvider);
        vscode.window.registerTreeDataProvider('dependency-analyzer', dependencyAnalyzerProvider);

        // 注册依赖分析命令 - 这是我们需要确保正常工作的关键命令
        context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.analyzeDependencies', async () => {
                console.log('依赖分析命令已触发');
                try {
                    await dependencyAnalyzer.analyzeDependencies();
                } catch (error) {
                    vscode.window.showErrorMessage(`依赖分析失败: ${error}`);
                    console.error("依赖分析错误:", error);
                }
            })
        );

        // 注册依赖分析相关的其他命令
        context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.openPomFile', (pomPath: string) => {
                if (pomPath) {
                    vscode.workspace.openTextDocument(pomPath).then(document => {
                        vscode.window.showTextDocument(document);
                    });
                }
            })
        );

        // 注册诊断命令
        context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.diagnoseSetup', async () => {
                try {
                    await mavenUtils.diagnoseMavenSetup();
                    vscode.window.showInformationMessage('Maven环境诊断已完成，请查看输出面板。');
                } catch (error) {
                    vscode.window.showErrorMessage(`Maven环境诊断失败: ${error}`);
                }
            })
        );

        // 注册其他必要命令
        context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.runMavenGoal', () => {
                mavenRunner.runMavenGoal(false);
            }),
            vscode.commands.registerCommand('maven-helper.runMavenGoalForRoot', () => {
                mavenRunner.runMavenGoal(true);
            }),
            vscode.commands.registerCommand('maven-helper.openTerminalAtModule', () => {
                mavenTerminal.openTerminalAtModule();
            }),
            vscode.commands.registerCommand('maven-helper.runCurrentTest', () => {
                testRunner.runCurrentTest(false);
            }),
            vscode.commands.registerCommand('maven-helper.debugCurrentTest', () => {
                testRunner.runCurrentTest(true);
            }),
            vscode.commands.registerCommand('maven-helper.refreshModules', () => {
                mavenModulesProvider.refresh();
            }),
            vscode.commands.registerCommand('maven-helper.refreshDependencies', () => {
                dependencyAnalyzerProvider.refresh();
            })
        );

        // 添加一个状态栏按钮
        const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
        statusBarItem.text = "$(list-unordered) Maven依赖分析";
        statusBarItem.tooltip = "分析Maven依赖";
        statusBarItem.command = "maven-helper.analyzeDependencies";
        statusBarItem.show();
        context.subscriptions.push(statusBarItem);

    } catch (error) {
        // 捕获并显示初始化过程中的任何错误
        console.error('Maven Helper 扩展初始化失败:', error);
        vscode.window.showErrorMessage(`Maven Helper 扩展初始化失败: ${error}`);
    }
}

export function deactivate() {}
